package com._520it.crm.service.impl;

import com._520it.crm.domain.*;
import com._520it.crm.mapper.*;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.AuditInsuranceQueryObject;
import com._520it.crm.service.IAuditInsuranceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AuditInsuranceServiceImpl implements IAuditInsuranceService {

    @Autowired
    private AuditInsuranceMapper auditInsuranceMapper;
    @Autowired
    private InsuranceInfoChangeMapper insuranceInfoChangeMapper;
    @Autowired
    private AuditInfoMapper auditInfoMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;
    @Autowired
    private AuditNotPassMapper auditNotPassMapper;




    @Override
    public int insert(AuditInsurance record) {
        return auditInsuranceMapper.insert(record);
    }


    @Override
    public PageResult query(AuditInsuranceQueryObject qo) {

        List<InsuranceInfoChange> IicLiat = insuranceInfoChangeMapper.queryForRows();

        List<String> orderBillNumberList = auditInsuranceMapper.queryOrderBillNumber();
        //循环遍历出所有已批改的单号,判断审核表中有没有包含单号,如果没有就保存到审核表中
        for (InsuranceInfoChange row : IicLiat) {
            if(!orderBillNumberList.contains(row.getOrderbill().getOrderBillNumber())){
                AuditInsurance ai = new AuditInsurance();
                ai.setInsuranceInfoChange(row);
                auditInsuranceMapper.insert(ai);
            }
        }

        Integer count = auditInsuranceMapper.queryForCount(qo);
        if (count == 0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<AuditInsurance> rows = auditInsuranceMapper.queryForList(qo);

        return new PageResult(count,rows);
    }


    @Override
    public void updateAuditTime(Long id) {
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();

        //提交审核时间到待审核表
        auditInsuranceMapper.updateAuditTime(new Date(),current.getSn(),id);

        //保存审核表
        auditInfoMapper.insertAuditId(id);

        //获取审核表中审核保单的ID
        Long auditId = auditInfoMapper.selectAuditId(id);



        if (current.getAdmin() == 1){
            String str = String.valueOf(System.currentTimeMillis());
            //更新超级管理员信息
            auditInfoMapper.updateAdminMsg(str,id);
            //查询出billId,然后进行修改状态和审核人
            Long  billId = auditInsuranceMapper.queryBillIdByAuditInsuranceId(id);
            OrderBill bill = new OrderBill();
            bill.setId(billId);
            bill.setState(OrderBill.STATE_HBYES);
            bill.setExamineUserSn(current.getSn());
            bill.setRemark("系统自核!");
            orderBillMapper.updateRemark(bill);

            AuditNotPass anp = new AuditNotPass();
            anp.setOrderBillId(billId);
            anp.setEmployeeSn(current.getSn());
            anp.setAuditInfoId(auditId);
            anp.setAuditTime(new Date());
            anp.setAuditText("同意");
            auditNotPassMapper.insert(anp);
        }

    }

    @Override
    public AuditInsurance auditHistoryData(Long id) {
        return auditInsuranceMapper.auditHistoryData(id);
    }
}
